package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Iterator;

import javax.servlet.http.*;



import utils.UsersInfo;

import models.Location;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionAdoption;
import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxLocationMatrixServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		PrintWriter out = resp.getWriter();
		

		out.println("<TABLE><TBODY><TR><TD width=100><SPAN class='lotusBtn lotusBtnAction lotusLeft'> <input type=\"hidden\" id=\"fileName\" name=\"fileName\" value=\"data.xls\" /> <A href='#' onclick=\"gotoExcel('tableData', 'tableHTML');\" >Export XL</A></SPAN></TD></TR></TBODY></TABLE>");		
		out.println("<P style='FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 14px; FONT-WEIGHT: bold' width=120px colspan='5'>Location Summary Report</P>");
		out.println("<div id=\"tableData\"> ");
		out.println("<TABLE  ><TBODY><TR><TD class=style0 width=100>Location</TD>");
		out.println("<TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; BACKGROUND: #639831; COLOR: #000000; VERTICAL-ALIGN: bottom; FONT-WEIGHT: bold'>Shared</TD><TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; BACKGROUND: #639831; COLOR: #000000; VERTICAL-ALIGN: bottom; FONT-WEIGHT: bold'>Adopted</TD></TR>");
		
		Objectify ofy = ObjectifyService.begin();
		Query<Location> qq = ofy.query(Location.class).order("LocationName");			
		for ( Location f : qq.fetch() ) {
			
			
			
			out.println("<TR><TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; BACKGROUND: #ff8100; COLOR: white; VERTICAL-ALIGN: bottom'>" + f.getLocationName() + "</TD>");
			
			Query<SolutionSubmission> ss = ofy.query(SolutionSubmission.class).filter("Location", f.getLocationName());
			Query<SolutionAdoption> sa = ofy.query(SolutionAdoption.class).filter("Location", f.getLocationName());
			
			if( ss.countAll() > 0 ){
				out.println("<TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; COLOR: #000000; VERTICAL-ALIGN: bottom; BORDER-RIGHT: black 1px solid' width=200>" + ss.countAll() + "</TD>");
			}else{
				out.println("<TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; COLOR: #000000; VERTICAL-ALIGN: bottom; BORDER-RIGHT: black 1px solid' width=200>.</TD>");
			}
				
			if( sa.countAll() > 0 ){
				out.println("<TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; COLOR: #000000; VERTICAL-ALIGN: bottom; BORDER-RIGHT: black 1px solid' width=200>" + sa.countAll() + "</TD>");
			}else{
				out.println("<TD style='BORDER-BOTTOM: black 1px solid; TEXT-ALIGN: center; COLOR: #000000; VERTICAL-ALIGN: bottom; BORDER-RIGHT: black 1px solid' width=200>.</TD>");
			}
			
			out.println("</TR>");
		} //LOCATION LOOP		
		

   		out.println("</TBODY></TABLE>");		
   		out.println(" </div> ");	
	}
}
